package com.zte.sys.dept.control;

import com.zte.sys.area.model.SysCity;
import com.zte.sys.area.model.SysProvince;
import com.zte.sys.dept.model.Dept;
import com.zte.sys.dept.model.DeptCondition;
import com.zte.sys.dept.model.SysCityCondition;
import com.zte.sys.dept.service.IDeptService;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptAction {

    @Autowired
    private IDeptService deptService;

    //分页查询
    @RequestMapping("/dept/pageDept")
    @ResponseBody
    @OperatorLog(operate="部门分页条件查询", module="部门管理")
    public ResultMessage pageDept(DeptCondition conn, PageModel<Dept> pageModel) {
        PageModel<Dept> depts = deptService.queryDeptPageByCondition(conn,pageModel);
        ResultMessage rm = new ResultMessage();
        if (depts != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(depts);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    //查省市
    @RequestMapping("/dept/provinceList")
    @ResponseBody
    public ResultMessage provinceList(String pId) {
        List<SysProvince> provinces = deptService.queryProvinceList(pId);
        ResultMessage rm = new ResultMessage();
        if (provinces != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(provinces);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    @RequestMapping("/dept/cityList")
    @ResponseBody
    public ResultMessage cityList(SysCityCondition conn) {
        List<SysCity> cities = deptService.queryCityList(conn);
        ResultMessage rm = new ResultMessage();
        if (cities != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(cities);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    //详情 主要用于数据回显
    @RequestMapping("/dept/deptDetail")
    @ResponseBody
    public ResultMessage queryDeptByDeptId(DeptCondition conn) throws Exception{
        Dept dept = null;
        if(conn!= null && !conn.equals("")){
            dept = deptService.queryDeptByDeptId(conn);
        }
        ResultMessage rm = new ResultMessage();
        if (dept != null) {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("部门已存在！");
            rm.setData(dept);
        } else {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("成功");
        }
        return rm;
    }

    //添加
    @RequestMapping("sys/dept/deptAdd")
    public String deptAdd(){
        return "sys/dept/deptAdd";
    }
    @RequestMapping("/dept/deptAdd")
    @ResponseBody
    @OperatorLog(operate="添加部门", module="部门管理")
    public ResultMessage addDept(Dept dept){
        int i = deptService.addDept(dept);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("添加成功！");
            rm.setData(dept);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("添加失败！");
        }
        return rm;
    }

    //修改
    @RequestMapping("sys/dept/deptUpdate")
    public String deptUpdate(){
        return "sys/dept/deptUpdate";
    }
    @RequestMapping("/dept/deptUpdate")
    @ResponseBody
    @OperatorLog(operate="修改部门", module="部门管理")
    public ResultMessage updateDept(Dept dept){
        int i = deptService.updateDept(dept);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(dept);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }

    @RequestMapping("/dept/updateDeptState")
    @ResponseBody
    @OperatorLog(operate="修改部门状态", module="部门管理")
    public ResultMessage updateStatusId(Dept dept){
        int i = deptService.updateDeptState(dept);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(dept);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }
}
